package com.pzl.hwr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.pzl.hwr.util.HciCloudSysHelper;
import com.pzl.hwr.util.Util;
import com.pzl.hwr.view.MyJTHwrPadView;
import com.sinovoice.hcicloudsdk.common.hwr.HwrConfig;
import com.sinovoice.hcicloudsdk.common.hwr.HwrInitParam;
import com.sinovoice.hcicloudui.hwr.JTHwrListener;
import com.sinovoice.hcicloudui.hwr.JTHwrPadView;

import java.util.ArrayList;

public class HWR2Activity extends AppCompatActivity {

    private static final String TAG = "HWR2Activity";

    private MyJTHwrPadView jtHwrPadView;

    private TextView tv;

    private HciCloudSetting hciCloudSetting;

    private HciCloudSysHelper mHciCloudSysHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hwr2);
        initView();

        hciCloudSetting = HciCloudSetting.getInstance();
        hciCloudSetting.loadSetting(this);

        mHciCloudSysHelper = HciCloudSysHelper.getInstance();
        mHciCloudSysHelper.init(this);

    }

    private void initView() {
        jtHwrPadView = (MyJTHwrPadView) findViewById(R.id.id_hwr_pad);
        tv = (TextView) findViewById(R.id.tv);
    }


    @Override
    protected void onResume() {
        super.onResume();

        boolean flag = jtHwrPadView.init(getHwrInitParam(), new JTHwrListener() {
            @Override
            public void onResult(ArrayList<String> resultList) {
                StringBuilder sb = new StringBuilder();
                for (String result : resultList) {
                    sb.append(result + "  ");
                }
                tv.setText(sb.toString());
                Log.d(TAG, sb.toString());
            }

            @Override
            public void onError(int errorCode, String details) {
                Toast.makeText(HWR2Activity.this, "code:" + errorCode + "\r\ndetails:"
                        + details, Toast.LENGTH_LONG).show();
            }
        });

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        HwrConfig hwrConfig = new HwrConfig();
        //fillLocalLetterHwrConfigParam(hwrConfig);
        //fillCloudFreeWriteHwrConfigParam(hwrConfig);
        fillLocalFreestylusHwrConfigParam(hwrConfig);

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        fillHwrViewParam(hwrConfig);

        jtHwrPadView.start(hwrConfig.getStringConfig());


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jtHwrPadView.release();
    }


    /**
     * 设置 HWR 手写识别的  云端多字识别功能的  手写配置参数
     *
     * @param hwrConfig
     */
    private void fillCloudFreeWriteHwrConfigParam(HwrConfig hwrConfig) {

        hwrConfig.addParam(HwrConfig.SessionConfig.PARAM_KEY_CAP_KEY, "hwr.cloud.freewrite");//云多字

        a(hwrConfig, "hwr.cloud.freewrite");

        //设置识别结果的候选个数
        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_CAND_NUM, "10");
        //设置识别结果的范围
        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_RECOG_RANGE, "gbk");
        //设置叠写还是行写 line是行写，overlap是叠写
        hwrConfig.addParam(HwrConfig.InputConfig.PARAM_KEY_SPLIT_MODE, "line");

    }

    /**
     * 设置 HWR 手写识别 本地多字识别功能
     *
     * @param hwrConfig
     */
    private void fillLocalFreestylusHwrConfigParam(HwrConfig hwrConfig) {

        hwrConfig.addParam(HwrConfig.SessionConfig.PARAM_KEY_CAP_KEY, "hwr.local.freestylus");//本地多字

        a(hwrConfig, "hwr.local.freestylus");

        //设置识别结果的候选个数
        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_CAND_NUM, "10");
        //设置识别结果的范围
        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_RECOG_RANGE, "gbk");
        //设置叠写还是行写 line是行写，overlap是叠写
        hwrConfig.addParam(HwrConfig.InputConfig.PARAM_KEY_SPLIT_MODE, "line");

    }

    /**
     * 设置 HWR 手写识别的  本地单字识别功能的  手写配置参数
     *
     * @param hwrConfig
     */
    private void fillLocalLetterHwrConfigParam(HwrConfig hwrConfig) {
        hwrConfig.addParam(HwrConfig.SessionConfig.PARAM_KEY_CAP_KEY, "hwr.local.letter");

        a(hwrConfig, "hwr.local.letter");

        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_CAND_NUM, "10");
        hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_DISP_CODE, "nochange");
        hwrConfig.addParam("realtime", "no");
        //hwrConfig.addParam(HwrConfig.PARAM_KEY_WORD_MODE, "capital");
    }


    /**
     * 给手写板设置参数
     *
     * @param hwrConfig
     */
    private void fillHwrViewParam(HwrConfig hwrConfig) {
        hwrConfig.addParam(JTHwrPadView.PARAM_COLOR_STYLE, JTHwrPadView.PARAM_COLOR_STYLE_LIGHT);
        hwrConfig.addParam(JTHwrPadView.PARAM_UP_TIME, "800");
        hwrConfig.addParam(JTHwrPadView.PARAM_STROKE_WIDTH, "20");
    }

    /**
     * 一个根据技术人员提供的demo,里面的一个不明的 设置参数的条件判断
     *
     * @param hwrConfig
     * @param capKey
     */
    private void a(HwrConfig hwrConfig, String capKey) {
        int bRet = capKey.indexOf("hwr.local.freestylus.v7");
        if (bRet == -1) {
            hwrConfig.addParam(HwrConfig.ResultConfig.PARAM_KEY_RECOG_RANGE, "all");//Ӣ����д��֧��all
            hwrConfig.addParam(HwrConfig.InputConfig.PARAM_KEY_SPLIT_MODE, "overlap");//Ӣ����д��֧��overlap
        }
    }


    /**
     * 获得 手写识别（HWR）的初始化参数
     *
     * @return
     */
    private String getHwrInitParam() {

        HwrInitParam initParam = new HwrInitParam();
        String filePath = getFilesDir().getAbsolutePath();
        String hwrDirPath = filePath.replace("files", "lib");
        initParam.addParam(HwrInitParam.PARAM_KEY_DATA_PATH, Util.HciCloudDataPath);
        Log.d(TAG, "Util.HciCloudDataPath = " + Util.HciCloudDataPath);
        //initParam.addParam(HwrInitParam.PARAM_KEY_AUTO_UPLOAD, "yes");
        initParam.addParam(HwrInitParam.PARAM_KEY_FILE_FLAG, HwrInitParam.VALUE_OF_PARAM_FILE_FLAG_NONE);

        return initParam.getStringConfig();
    }

}
